package com.example.tool.ui;

import com.example.tool.utils.SwingUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.ui.JBColor;
import com.intellij.ui.components.JBTextField;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.util.ui.JBUI;
import org.jetbrains.annotations.Nullable;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>Title: ActionsDialogWrapper</p>
 * Description: actions中相关的ui
 * <p>CreateTime: 2022/8/19 15:14</p>
 * @author FT
 */
public class ActionsDialogWrapper extends DialogWrapper {
  private final Project project;
  private final String[] names;
  private List<String> titleValue;
  private List<String> value;
  private final List<JComponent> jbTextFieldList = new ArrayList<>();
  private JComponent focusJComponent;
  private Function<List<JComponent>, Boolean> functionList = null;

  public ActionsDialogWrapper(Function<List<JComponent>, Boolean> function, Project project, String... names) {
    this(project, names);
    functionList = function;
  }

  public ActionsDialogWrapper(Function<List<JComponent>, Boolean> function, Project project, List<TitleCA> titleCAList) {
    this(project, titleCAList);
    functionList = function;
  }

  public ActionsDialogWrapper(Project project, List<TitleCA> titleCAList) {
    super(true);
    this.project = project;
    List<String> title = new ArrayList<>();
    List<String> value = new ArrayList<>();
    titleCAList.forEach(titleCA -> {
      title.add(titleCA.getTitle());
      value.add(titleCA.getValue());
    });
    this.titleValue = value;
    this.names = title.toArray(new String[0]);
    setTitle("Create File");
    setSize(450, 160);
    setResizable(false);
    init();
  }

  public ActionsDialogWrapper(Project project, String... names) {
    super(true);
    this.project = project;
    this.names = names;
    setTitle("Create File");
    setSize(450, 160);
    setResizable(false);
    init();
    /*
     取消显示
     if (names.length == 0) {
       super.doCancelAction();
     }
    */
  }

  @Override
  public @Nullable JComponent getPreferredFocusedComponent() {
    return focusJComponent;
  }

  public List<String> getValue() {
    return value;
  }

  @Override
  protected @Nullable JComponent createCenterPanel() {
    if (names.length != 0) {
      JPanel jPanel = new JPanel();
      jPanel.removeAll();
      jPanel.setLayout(new GridLayoutManager(names.length + 1, 2));
      for (int i = 0; i < names.length; i++) {
        if (names[i].startsWith("[choice]")) {
          String regex = "[^(\\[choice\\])].*";
          Pattern pattern = Pattern.compile(regex);
          Matcher matcher = pattern.matcher(names[i]);
          if (matcher.find()) {
            JCheckBox jCheckBox = new JCheckBox();
            jCheckBox.setText(matcher.group(0));
            jCheckBox.setSelected(true);
            SwingUtil.addChild(jPanel, jCheckBox, i);
            jbTextFieldList.add(jCheckBox);
          }
        } else {
          SwingUtil.addChild(jPanel, new JLabel(names[i]), i, 0);
          JBTextField jComponent = new JBTextField();
          if (null != titleValue) {
            jComponent.setText(titleValue.get(i));
          }
          jbTextFieldList.add(jComponent);
          SwingUtil.addChild(jPanel, jComponent, i, 1, true);
          if (i == 0) {
            this.focusJComponent = jComponent;
          }
        }
      }
      JPanel tips = tips(() -> {
        // 扩展提示文字
        String otherTips = SwingUtil.promptTextActionTips();
        String strMsg;
        if (otherTips == null) {
          strMsg = "<html><body>" + SwingUtil.promptTextActionAll() + "</body></html>";
        } else {
          strMsg = "<html><body>" + SwingUtil.promptTextActionAll() + "<br>" + otherTips + "</body></html>";
        }
        return strMsg;
      });
      SwingUtil.addChild(jPanel, tips, names.length);
      return jPanel;
    } else {
      return tips(() -> {
        // 扩展提示文字
        String otherTips = SwingUtil.promptTextActionTips();
        return "<html><body>" + Objects.requireNonNullElseGet(otherTips, SwingUtil::promptTextActionAll) + "</body></html>";
      });
    }
  }

  protected JPanel tips(Supplier<String> supplier) {
    JPanel tips = new JPanel();
    tips.setLayout(new GridLayoutManager(1, 2, JBUI.insets(10), -1, -1));
    tips.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new JBColor(new Color(-10591639), new Color(-10591639))), "说明"));
    String strMsg = supplier.get();
    SwingUtil.addChild(tips, new JLabel(strMsg), 0);
    return tips;
  }

  @Override
  protected void doOKAction() {
    List<String> content = new ArrayList<>();
    jbTextFieldList.forEach(jbTextField -> {
      if (jbTextField instanceof JBTextField) {
        content.add(((JBTextField) jbTextField).getText());
      } else if (jbTextField instanceof JCheckBox) {
        boolean selected = ((JCheckBox) jbTextField).isSelected();
        content.add(String.valueOf(selected));
      }
    });
    boolean match = content.stream().noneMatch(str -> str.equals(""));
    if (match) {
      this.value = content;
      if (functionList != null) {
        if (functionList.apply(jbTextFieldList)) {
          super.doOKAction();
        }
      } else {
        super.doOKAction();
      }
    } else {
      Messages.showMessageDialog(project, "Please fill in all Input box!", "Error", HToolIcon.ModuleIcon);
    }
  }
}
